<?php
namespace app\command\gateway;

use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
// use think\facade\Config;
use think\facade\Cache;
use Workerman\Connection\AsyncTcpConnection;
use Workerman\Connection\TcpConnection;
use Workerman\Worker;

// use Workerman\Lib\Timer;
// use think\facade\Log;

class MyGateway extends Command
{
    public function configure()
    {
        $this->setName('mygateway')
            ->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start') //默认start开始
        // ->addOption('host', 'H', Option::VALUE_OPTIONAL, 'the host of workerman server.', null)//ip
        // ->addOption('port', 'p', Option::VALUE_OPTIONAL, 'the port of workerman server.', null)//端口
            ->addOption('daemon', 'd', Option::VALUE_NONE, 'Run the workerman server in daemon mode.') //守护进程
            ->setDescription('GatewayWorker Server for ThinkPHP');
    }
    public function execute(Input $input, Output $output)
    {
        $action = $input->getArgument('action');
        //判断传入的指令
        if (DIRECTORY_SEPARATOR !== '\\') {
            if (!in_array($action, ['start', 'stop', 'reload', 'restart', 'status', 'connections'])) {
                $output->writeln("Invalid argument action:{$action}, Expected start|stop|restart|reload|status|connections .");
                exit(1);
            }

            global $argv;
            array_shift($argv);
            array_shift($argv);
            array_unshift($argv, 'think', $action);
        } else {
            $output->writeln("GatewayWorker Not Support On Windows.");
            exit(1);
        }

        if ('start' == $action) {
            $output->writeln('Starting GatewayWorker server...');
        }
        $this->start();
    }
    private function start()
    {
        /* 注册地址 */
        $this->register('127.0.0.1:1290');
        /* 业务进程 */
        $this->businessWorker();
        /* 开启端口 */
        $this->startTowerGateWay();
        Worker::runAll();

    }
    private function register(string $registerAddress)
    {
        new Register('text://' . $registerAddress);
    }
    private function businessWorker()
    {
        // 初始化 bussinessWorker 进程
        $worker = new BusinessWorker();
        $worker->name = 'bussinessWorker';
        $worker->count = 4; //进程数量
        $worker->registerAddress = '127.0.0.1:1290';
        $worker->eventHandler = 'Events';
    }
    private function startTowerGateWay()
    {
        $gateway = new Gateway("tcp://0.0.0.0:19000");
        $gateway->name = 'towerTCP';
        $gateway->count = 2;
        $gateway->lanIp = '127.0.0.1';
        $gateway->startPort = 19010; //起始端口号
        $gateway->pingInterval = 55; //客户端发送给服务端主动检测心跳周期
        $gateway->pingNotResponseLimit = 0; //1代表客户端必须定时发送数据给服务端 0则不检测
        $gateway->pingData = ''; //服务端主动发送心跳给客户端的内容
        $gateway->registerAddress = '127.0.0.1:1290';
    }
    private function startElevatorGateWay()
    {
        $gateway = new Gateway("tcp://0.0.0.0:2450");
        $gateway->name = 'elevatorTCP';
        $gateway->count = 2;
        $gateway->lanIp = '127.0.0.1';
        $gateway->startPort = 2460; //起始端口号
        $gateway->pingInterval = 55; //客户端发送给服务端主动检测心跳周期
        $gateway->pingNotResponseLimit = 0; //1代表客户端必须定时发送数据给服务端 0则不检测
        $gateway->pingData = ''; //服务端主动发送心跳给客户端的内容
        $gateway->registerAddress = '127.0.0.1:1236';
    }
    private function startGateAnticollision()
    {
        $gateway = new Gateway("tcp://0.0.0.0:4400");
        $gateway->name = 'tcpAnticollision';
        $gateway->count = 8;
        $gateway->lanIp = '127.0.0.1';
        $gateway->startPort = 4446;
        $gateway->pingInterval = 55;
        $gateway->pingNotResponseLimit = 1;
        $gateway->pingData = '';
        $gateway->registerAddress = '127.0.0.1:1236';
    }
    private function startTest()
    {
        $gateway = new Gateway("tcp://0.0.0.0:8848");
        $gateway->name = 'startTest';
        $gateway->count = 2;
        $gateway->lanIp = '127.0.0.1';
        $gateway->startPort = 8840;
        $gateway->pingInterval = 55;
        $gateway->pingNotResponseLimit = 1;
        $gateway->pingData = '';
        $gateway->registerAddress = '127.0.0.1:1236';
    }
    public function AsyncTcpConnection()
    {
        $worker = new Worker('tcp://0.0.0.0:8992');
        $worker->name = 'AsyncTcpConnection-Worker';
        $worker->count = 1;
        $worker->onWorkerStart = function ($worker) {
            echo 'AsyncTcp Connection start';
        };
        // tcp连接建立后
        $worker->onConnect = function (TcpConnection $connection) {
            // 建立本地80端口的异步连接
            $connection_to_2450 = new AsyncTcpConnection('tcp://0.0.0.0:2450');
            // 设置将当前客户端连接的数据导向2450端口的连接
            $connection->pipe($connection_to_2450);
            // 设置2450端口连接返回的数据导向客户端连接
            $connection_to_2450->pipe($connection);
            // 执行异步连接
            $connection_to_2450->connect();
        };
    }
}
